package com.wh.wisdomsite.xbox.ba.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.wh.wisdomsite.xbox.ba.dao.MenuMapper;
import com.wh.wisdomsite.xbox.ba.entity.Menu;
import com.wh.wisdomsite.xbox.ba.service.IMenuService;
import com.wh.wisdomsite.xbox.utils.date.DateTool;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 菜单ServiceImpl
 *
 * @version V1.0
 * @Package: com.wh.wisdomsite.xbox.us.service.impl
 * @author: 丁奕
 * @date: 2018-08-03 11:42
 * @Copyright 公司版权所有
 * @Company 贵州万华科技有限公司Copyright (c) 2016
 **/
@Service("menuService")
@Transactional(rollbackFor = {Exception.class,RuntimeException.class})
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {

    private static final Log logger = LogFactory.getLog(MenuServiceImpl.class);

    @Autowired
    private MenuMapper menuMapper;

    /**
     * 返回树形菜单列表
     * @return treeMenuList
     */
    @Override
    public List<Menu> getTreeMenu() {
        //设置条件
        EntityWrapper<Menu> ewMenu = new EntityWrapper<Menu>();
        ewMenu.setEntity(new Menu());
        ewMenu.orderBy("sort",true);
        //先查询出所有的菜单LIST
        List<Menu> menuList = menuMapper.selectList(ewMenu);
        //将menuList传化成树形的treeMenuList
        List<Menu> treeMenuList = toTreeMenuList(menuList);
        return treeMenuList;
    }

    /**
     * 将menuList转换成树形的treeMenuList
     * @param menuList
     * @return
     */
    @Override
    public List<Menu> toTreeMenuList(List<Menu> menuList) {
        List<Menu> treeMenuList = new ArrayList<>();
        //先装载根节点
        for (Menu menu : menuList) {
            if(menu.getParentId().equals("0")){
                //在根节点的List中加入此menu
                menu.setCreateDateFormat(DateTool.format(menu.getCreateDate(), "yyyy-MM-dd HH:mm:ss"));
                treeMenuList.add(menu);
            }
        }
        //传入根节点List,装载其子节点
        treeMenuList = loadChildren(treeMenuList, menuList);
        return treeMenuList;
    }

    /**
     * 根据传入的父级节点LIST,以及要遍历的menuList，去构建树形结构
     * @param parentMenuList
     * @param menuList
     * @return
     */
    private List<Menu> loadChildren(List<Menu> parentMenuList, List<Menu> menuList) {
        for (Menu menu : parentMenuList){
            List<Menu> childrenMenuList = new ArrayList<>();
            for(Menu menu1 : menuList){
                if(menu1.getParentId().equals(menu.getMeId())){
                    menu1.setCreateDateFormat(DateTool.format(menu.getCreateDate(), "yyyy-MM-dd HH:mm:ss"));
                    childrenMenuList.add(menu1);
                }
            }
            menu.setChildren(childrenMenuList);
            //迭代添加子菜单
            loadChildren(childrenMenuList, menuList);
        }
        return parentMenuList;
    }

    /**
     * 删除该菜单之前,先查询是否有子菜单，若有则不删除
     * @param id
     * @return
     */
    @Override
    public boolean deleteById(Serializable id) {
        //获取带子菜单的menu
        Menu menu = getMenuWithChildren(id);
        List<Menu> childrenMenu = menu.getChildren();
        if(!childrenMenu.isEmpty()){
            return false;
        }
        return super.deleteById(id);
    }

    /**
     * 查询出带子菜单的menu
     * @param id
     * @return menu
     */
    @Override
    public Menu getMenuWithChildren(Serializable id) {
        Menu menu = super.selectById(id);
        List<Menu> menuList = new ArrayList<Menu>();
        menuList.add(menu);
        //设置条件
        EntityWrapper<Menu> ewMenu = new EntityWrapper<Menu>();
        ewMenu.setEntity(new Menu());
        ewMenu.orderBy("sort",true);
        //先查询出所有的菜单LIST
        List<Menu> allMenuList = menuMapper.selectList(ewMenu);
        //遍历上级菜单然后插入子菜单列表，迭代遍历
        List<Menu> treeMenuList = loadChildren(menuList, allMenuList);
        return treeMenuList.get(0);
    }

    @Override
    public Map<String, Object> getMapById(Serializable id) {
        return menuMapper.getMapById(id);
    }

    @Override
    public List<Menu> getMenuListByRoleId(String id) {
        List<Menu> menuList  = menuMapper.getMenuListByRoleId(id);
        return menuList;
    }

    @Override
    public List<Menu> getMenuListByUsId(String usId) {
        List<Menu> menuList = menuMapper.getTreeMenuByUsId(usId);
        return menuList;
    }

    /**
     * 验证权限标识是否已存在
     * @param value
     * @param oldValue
     * @return
     */
    @Override
    public boolean permissionIsExists(String value, String oldValue) {
        //判断是否有原始值，有原始值则为修改, 且原始值跟新值不相同，才进行查询，否则直接返回false
        if(StringUtils.isNotEmpty(oldValue) && value.equals(oldValue)){
            return false;
        }
        //设置条件
        EntityWrapper<Menu> ew = new EntityWrapper<>();
        ew.setEntity(new Menu());
        ew.eq("permission", value);
        return menuMapper.selectCount(ew) > 0;
    }

}
